package com.cdl.utils;

import com.cdl.utils.bean.*;

import java.util.*;

public class ColumnInfoUtil {

	private static Properties properties = PropertyUtil.InitProp();

	private static TableInfoUtil tableInfoUtil ;

	static{
		tableInfoUtil = new JDBCUtil();
	}

	/*****
	 * 返回表信息，包含列名、索引信息
	 * @param tableName
	 * @return
	 */
	public static TableInfo getTableInfo(String tableName){
		if(!tableInfoUtil.isExistTable(tableName)){
			return null ;
		}
		TableInfo t = new TableInfo();
		List<IndexInfo> inds = new ArrayList<IndexInfo>();
		inds = tableInfoUtil.getTableIndexs(tableName) ;
		t.setTableName(tableName);
		t.setLowerTableName(tableName.toLowerCase());
		t.setClassName(StringUtil.getClassString(tableName));
		t.setLowerClassName(StringUtil.getFirstCharLower(t.getClassName()));
		t.setColumns(initColumnInfo(tableInfoUtil.getTableColumns(tableName)));
		t.setIndexs(inds);
		for(IndexInfo ind:inds ){
			if(ind.isUnique()){
				t.setUniqueIndex(ind);
				break;
			}
		}
		handleColumnInfo(t.getColumns());
		return t ;
	}
	
    /***
	 * 根据字段类型设置对应的bean属性和类型
	 * @param
	 * @return
	 */
	public static List<ColumnInfo> initColumnInfo(List<ColumnInfo> cols){
		for(ColumnInfo c:cols){
			String columnType = c.getColumnType().toUpperCase();
			c.setFieldName(StringUtil.getFiledString(c.getColumnName()));
			int len = c.getColumnLength() ;
			int scale = c.getColumnScale() ;
			if("INTEGER".equals(columnType)
					||"INT".equals(columnType) ){
				c.setFieldType("Integer");
				c.setJdbcType("NUMERIC");
			}else if( "LONG".equals(columnType)){
				c.setFieldType("Long") ;
				c.setJdbcType("NUMERIC");
			}else if("NUMBER".equals(columnType)){
				c.setFieldType(scale==0?(len<10?"Integer":(len<18?"Long":"BigDecimal")):"BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "FLOAT".equals(columnType)
					||"FLOAT PRECISION".equals(columnType)
					||"DOUBLE".equals(columnType)
					||"BINARY_DOUBLE".equals(columnType)
					||"DOUBLE PRECISION".equals(columnType)
					){
				c.setFieldType("BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "DECIMAL".equals(columnType)
					||"REAL".equals(columnType)
					){
				//c.setFieldType(scale==0?(c.getColumnLength()<10?"Integer":"Long"):"BigDecimal");
				c.setFieldType("BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "VARCHAR".equals(columnType)
					||"VARCHAR2".equals(columnType)
					||"CHAR".equals(columnType)
					||"NVARHCAR".equals(columnType)
					||"NVARCHAR2".equals(columnType)
					||"NCHAR".equals(columnType)
					){
				c.setFieldType("String");
				c.setJdbcType("VARCHAR");
			}else if( "DATETIME".equals(columnType)
					||"DATE".equals(columnType)
					||columnType.contains("TIMESTAMP")
					){
				c.setFieldType("Date");
				c.setJdbcType("DATE");
			}else{
				c.setFieldType("String");
				c.setJdbcType("VARCHAR");
			}
			if("Date".equals(c.getFieldType())){
				c.setDataType(DataType.DATE);
			}else if("Integer".equals(c.getFieldType())){
				c.setDataType(DataType.INTEGER);
			}
			
		}
		return cols ;
	}
	
	/***
	 * 处理 tableInfo 中的ColumnInfo的枚举类型属性的值
	 * @param
	 * @return
	 */
	public static void handleColumnInfo(List<ColumnInfo> cols){
		String s = properties.getProperty("dataSet").trim() ;
		String[] select = null ;
		if(s != null && !"".equals(s) ){
			select = s.split(",");
		}else{
			return ;
		}
		List<DataSet<HashMap<String,String>>> list = parseDataSet();
		for(ColumnInfo col:cols){
			String filedName = col.getFieldName();
			for(DataSet<HashMap<String, String>> l:list){
				if(filedName.equals(l.getFieldName())){
					col.setDataType(l.getDataType());
					col.setOptions(l.getOptions());
				}
			}
		}
	}
	
	
	/***
	 * 解析特殊数据类型配置
	 * @return
	 */
	public static List parseDataSet(){
		
		List<DataSet<Map<String,String>>> l = new ArrayList<DataSet<Map<String,String>>>();
		String data = properties.getProperty("dataSet").trim();
		if(data!=null && !"".equals(data)){
			String[] dv = data.split(";");
			for(String v:dv){
				if(v==null || "".equals(v)){
					continue;
				}
				String[] ds = v.split(",");
				DataSet<Map<String,String>> od = new DataSet<Map<String,String>>();
				for(int i=0;i<ds.length;i++){
					switch(i){
					case 0:
						od.setFieldName(ds[0]);
						break ;
					case 1:	
						String dy = ds[1].toUpperCase() ;
						if("SELECT".equals(dy)){
							od.setDataType(DataType.SELECT);
						}else if("DATE".equals(dy)){
							od.setDataType(DataType.DATE);
						}else if("AMOUNT".equals(dy)){
							od.setDataType(DataType.AMOUNT);
						}else if("INTEGER".equals(dy)){
							od.setDataType(DataType.INTEGER);
						}
						break ;
					case 2:	
						if(!"".equals(ds[2]) && ds[2]!=null){
						List<Map<String,String>> list = new ArrayList<Map<String,String>>();
						for(String option:ds[2].split("&")){
							 Map<String,String> o = new HashMap<String,String>(); 
							 if(option!=null && !"".equals(option)){
								 String[] kv = option.split(":");
								 if(kv!=null && !"".equals(kv) ){
									 o.put(kv[0], kv[1]);	 
								 }
							 }
							 list.add(o);
						}
						od.setOptions(list);
						break ;
					}
					}
				}
				l.add(od);
			}
			
		}
		return l ;
	}
	
	
	
	
	public static void main(String[] args) {
		
		
		//System.out.println(getTableInfo("sys_para").getColumns());
		System.out.println(parseDataSet());
		
	}
	
}
